<?php

namespace App\Http\Controllers\Api\Customer;

use App\Facades\DeviceService;
use App\Facades\WxUserService;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class DeviceController extends Controller
{
    
	public function bind(Request $request)
	{

		$rules = [
				'device_code' => 'required|devicecode',
			];
		$this->validate($request, $rules);
		
		DeviceService::bind(strtolower($request->device_code));
        WxUserService::addDefaultUserinfo();
		return formatJsonResponse();
	}

	public function unbindByDeviceCode(Request $request)
	{
		$rules = [
				'device_code' => 'required',
			];
		$this->validate($request, $rules);
		
		$deviceCode = DeviceService::unbindByDeviceCode($request->device_code);
		return formatJsonResponse([
            'newDeviceCode' => $deviceCode,
            ]);
	}

	public function unbindCurrentDeviceCode(Request $request){
		$deviceCode = DeviceService::unbindByDeviceCode();
		return formatJsonResponse([
            'newDeviceCode' => $deviceCode,
            ]);
	}

    public function getState(Request $request){
    	$state = DeviceService::getState();
    	return formatJsonResponse([
    		'decStatus' => $state['decStatus'],
    		'decStatusDescription' => $state['decStatusDescription'],
    		'effluentStatus' => $state['effluentStatus'],
    		'effluentStatusDescription' => $state['effluentStatusDescription'],
    		'TDS1' => $state['TDS1'],
    		'TDS2' => $state['TDS2'],
    		'waterTemperature' => $state['waterTemperature'],
    		'filter1' => $state['filter1'],
    		'filterCnt1' => $state['filterCnt1'],
    		'filter2' => $state['filter2'],
    		'filterCnt2' => $state['filterCnt2'],
    		'waterCnt' => $state['waterCnt'],
    		]);
    }

    public function control(Request $request)
    {
        $rules = [
            'command' => 'required',
        ];
        $this->validate($request, $rules);
        
        DeviceService::control($request->command, $request->num);
        
        return formatJsonResponse();
    }

    public function change(Request $request)
    {
    	$rules = [
    		'device_code' => 'required',
    	];
    	$this->validate($request, $rules);

    	DeviceService::change($request->device_code);
    	return formatJsonResponse();
    }

}
